﻿function ChangePinion1(e) {
    var spur = document.getElementById("Vehicle_Spur1").value;
    var transRatio = document.getElementById("TransRatio").value;
    var pinion = e.newValue;

    document.getElementById("FDR1").innerText = Math.round(((spur / pinion) * transRatio) * 100) / 100;

}

function GetRatios() {
    var TransRatio = document.getElementById("TransRatio").value;
    var IdealRatio = document.getElementById("IdealRatio").value;
    var SpurCount = document.getElementById("SpurCount").value;

    // We know what the ideal ratio, TransRatio, and spur gears are now
    // we need to figure out the pinion gear sizes using this equation:
    // Pinion = (Spur / (IdealRatio / TransRatio)
    var Spur = null;

    for (i = 1; i <= SpurCount; i++) {
        Spur = document.getElementById("Vehicle_Spur" + i).value;
        var Pinion = Math.round((Spur / (IdealRatio / TransRatio)));

        FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
        document.getElementById("FDR" + i).innerHTML = FDR;
        document.getElementById("Pinion" + i).value = Pinion;
        ChangeTire(i);
    }
}

function ChangePinion(e, UpOrDown) {
    var TransRatio = document.getElementById("TransRatio").value;
    switch (e) {
        case 1:
            var Spur = document.getElementById("Vehicle_Spur1").value;
            var Pinion = document.getElementById("Pinion1").value;
            if (UpOrDown == "up") {
                Pinion++;
            } else if (UpOrDown == "down") {
                Pinion--;
            }
            FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
            document.getElementById("FDR1").innerHTML = FDR;
            document.getElementById("Pinion1").value = Pinion;
            break;
        case 2:
            var Spur = document.getElementById("Vehicle_Spur2").value;
            var Pinion = document.getElementById("Pinion2").value;
            if (UpOrDown == "up") {
                Pinion++;
            } else if (UpOrDown == "down") {
                Pinion--;
            }
            FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
            document.getElementById("FDR2").innerHTML = FDR;
            document.getElementById("Pinion2").value = Pinion;
            break;
        case 3:
            var Spur = document.getElementById("Vehicle_Spur3").value;
            var Pinion = document.getElementById("Pinion3").value;
            if (UpOrDown == "up") {
                Pinion++;
            } else if (UpOrDown == "down") {
                Pinion--;
            }
            FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
            document.getElementById("FDR3").innerHTML = FDR;
            document.getElementById("Pinion3").value = Pinion;
            break;
        case 4:
            var Spur = document.getElementById("Vehicle_Spur4").value;
            var Pinion = document.getElementById("Pinion4").value;
            if (UpOrDown == "up") {
                Pinion++;
            } else if (UpOrDown == "down") {
                Pinion--;
            }
            FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
            document.getElementById("FDR4").innerHTML = FDR;
            document.getElementById("Pinion4").value = Pinion;
            break;
    }
    ChangeTire(e);
}

function ChangeTire(e) {
    var diameter = document.getElementById("tire" +e).value;
    document.getElementById("tireDiameter" + e).value = diameter;
    var motorRPM = document.getElementById("rpm").value;
    var ratio = document.getElementById("FDR"+e).innerHTML;

    var topSpeed = motorRPM / ratio * Math.PI * diameter / 1056;

//    document.getElementById("mphTopSpeed" + e).value = round(topSpeed, 2);
//    document.getElementById("kphTopSpeed" + e).value = round(topSpeed* 1.6, 2);

    document.getElementById("mphTopSpeed2" + e).innerText= round(topSpeed, 2);
    document.getElementById("kphTopSpeed2" + e).innerText= round(topSpeed * 1.6, 2);
}

//ROUND A NUMBER (n) TO A PRECISION (p)
function round(n, p) {
    p = eval('1e' + p);
    return Math.round(n * p) / p;
}

